﻿/** 
 * Copyright 2010 NUMKIT.COM (contact: opensource@numkit.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @author 陈超
 * @version 1.0
 */
class Background {
	private static var formatter:TextFormat;
	public function Background() {
	}
	public function draw(mc:MovieClip, config:Config):Void {
		if (formatter == undefined) {
			formatter = new TextFormat();
			formatter.size = 12;
			formatter.color = config.textColor;
		}
		//background
		mc.beginFill(config.backgroundColor, 100);
		mc.moveTo(0, 0);
		mc.lineTo(0, Stage.height);
		mc.lineTo(Stage.width, Stage.height);
		mc.lineTo(Stage.width, 0);
		mc.lineTo(0, 0);
		mc.endFill();
		//cell
		mc.lineStyle(1, config.cellColor, 100, true, "normal", "square", "miter", 0);
		for (var y:Number = Stage.height-20-config.ySlide+Math.ceil((Stage.height-36)/config.cellHeight)*config.cellHeight; y>16; y -= config.cellHeight) {
			if (y<Stage.height-20) {
				mc.moveTo(20+config.leftSpace, y);
				mc.lineTo(Stage.width-16, y);
			}
		}
		for (var x:Number = 20+config.leftSpace+config.xSlide-Math.ceil((Stage.width-config.leftSpace-36)/config.cellWidth)*config.cellWidth; x<Stage.width-16; x += config.cellWidth) {
			if (x>20+config.leftSpace) {
				mc.moveTo(x, Stage.height-20);
				mc.lineTo(x, 16);
			}
		}
		//border
		mc.lineStyle(1, config.cellColor, 100, true, "normal", "square", "miter", 0);
		mc.moveTo(20+config.leftSpace, Stage.height-20);
		mc.lineTo(Stage.width-16, Stage.height-20);
		mc.lineTo(Stage.width-16, 16);
		mc.lineTo(20+config.leftSpace, 16);
		//yAxis
		mc.lineStyle(2, config.yAxisColor, 100, true, "normal", "square", "miter", 0);
		mc.lineTo(20+config.leftSpace, Stage.height-20);
		//xAxis
		var xAxisY:Number = Stage.height-(config.xAxisYPos-config.yFrom)*(Stage.height-36)/(config.yTo-config.yFrom)-20;
		if(xAxisY>=16&&xAxisY<=Stage.height-20){
			mc.lineStyle(1, config.xAxisColor, 100, true, "normal", "square", "miter", 0);
			mc.moveTo(20+config.leftSpace, xAxisY);
			mc.lineTo(Stage.width-16, xAxisY);
		}
		//markLine
		for(var i:Number=0;i<config.aMarkLineValue.length;i++){
			var markY:Number = Stage.height-(config.aMarkLineValue[i]-config.yFrom)*(Stage.height-36)/(config.yTo-config.yFrom)-20;
			if(markY>=16&&markY<=Stage.height-20){
				mc.lineStyle(1, config.aMarkLineColor[i], 100, true, "normal", "square", "miter", 0);
				mc.moveTo(20+config.leftSpace, markY);
				mc.lineTo(Stage.width-16, markY);
			}
		}
		
		//xLabel
		if (config.aXLabel.length>1) {
			var step:Number = (Stage.width-config.leftSpace-36)/(config.aXLabel.length-1);
			for (var i:Number = 0; i<config.aXLabel.length; i++) {
				var fontSize:Object = formatter.getTextExtent(config.aXLabel[i]);
				var xPos:Number = 20+config.leftSpace+step*i+config.xSlide;
				if (xPos<20+config.leftSpace || xPos>Stage.width-16) {
					continue;
				}
				var tField = mc.createTextField("tField", mc.getNextHighestDepth(), xPos-fontSize.textFieldWidth/2, Stage.height-20+2, fontSize.textFieldWidth, fontSize.textFieldHeight);
				tField.selectable = false;
				tField.text = config.aXLabel[i];
				tField.setTextFormat(formatter);
			}
		}
		//yLabel
		if (config.aYLabel.length>1) {
			var step:Number = (Stage.height-36)/(config.aYLabel.length-1);
			for (var i:Number = 0; i<config.aYLabel.length; i++) {
				var fontSize:Object = formatter.getTextExtent(config.aYLabel[i]);
				var yPos:Number = Stage.height-20-i*step-config.ySlide;
				if (yPos<16 || yPos>Stage.height-20) {
					continue;
				}
				var tField = mc.createTextField("tField", mc.getNextHighestDepth(), 20+config.leftSpace-2-fontSize.textFieldWidth, yPos-10, fontSize.textFieldWidth, fontSize.textFieldHeight);
				tField.selectable = false;
				tField.text = config.aYLabel[i];
				tField.setTextFormat(formatter);
			}
		}
	}
}
